package com.tbit.uqbike.service.event.handler;

import com.tbit.uqbike.constant.BrandRemoteConfigConstant;
import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.service.business.OutAgeService;
import com.tbit.uqbike.service.event.EventType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 品牌一键停运配置修改处理器
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-6
 */
@Component
@Slf4j
public class ServiceSwitchCloseHandler extends EventHandler{

    @Resource
    private OutAgeService outAgeService;

    @Override
    protected boolean match(Object config, EventType eventType) {
        if (Objects.isNull(config) || !(config instanceof BrandRemoteConfig)){
            return false;
        }
        BrandRemoteConfig brandRemoteConfig = (BrandRemoteConfig) config;
        return BrandRemoteConfigConstant.SERVICE_SWITCH.equals(brandRemoteConfig.getConfigType());
    }


    /**
     * 处理品牌一键停运配置变更事件
     * @param config
     * @param eventType
     */
    @Override
    protected void doHandle(Object config, EventType eventType) {
        if (!(config instanceof  BrandRemoteConfig)){
            return;
        }
        BrandRemoteConfig brandRemoteConfig = (BrandRemoteConfig) config;
        log.info("处理品牌一键停运配置变更事件：{},事件类型:{}", brandRemoteConfig, eventType);
        Integer brandId = brandRemoteConfig.getAccountId();
        if (Objects.isNull(brandId) || Objects.isNull(brandRemoteConfig.getParamKV())) {
            return;
        }
        //获取品牌一键停运配置 停运开关如果为关闭则删除停运配置信息
        String s = brandRemoteConfig.getParamKV();
        if ("1".equals(s.substring(s.indexOf("=") + 1))){
            outAgeService.deleteByAccountId(brandId, null);
        }
    }
}
